package com.zl.zsk.pojo.vo;

import lombok.Data;
import org.springframework.data.domain.Page;

import java.util.List;

@Data
public class PageInfo<T> {
    //当前页
    private int pageIndex;

    //每页多少条记录
    private int pageSize;

    //总页数
    private int totalPages;

    //总条数
    private int totalCount;

    //分页数据
    private List<T> list;

    public static PageInfo buildPageInfo(Page esPage, int pageIndex, int pageSize){
        final PageInfo<?> pageInfo = new PageInfo<>();
        if(esPage!=null){
            int totalCount = Integer.parseInt(String.valueOf(esPage.getTotalElements()));
            pageInfo.setTotalCount(totalCount);
            pageInfo.setPageIndex(pageIndex);
            pageInfo.setPageSize(pageSize);
            int totalPage = totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
            pageInfo.setTotalPages(totalPage);
            pageInfo.setList(esPage.getContent());
        }
        return pageInfo;
    }
}
